home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / tcsel003.zip / SORTDEMO.PAS < prev    next >
Pascal/Delphi Source File  |  1992-10-16  |  2KB  |  76 lines

  1. unit qsort;
  2.  
  3. interface
  4.  
  5. procedure quicksort(var s; left,right : word);
  6.  
  7. implementation
  8.  
  9. procedure quicksort(var s; left,right : word; SortType: stype);
  10.   { On the first call left should always be = to min and right = to max }
  11.   var
  12.     data      : DataArr absolute s;
  13.     pivotStr,
  14.     tempStr   : string;
  15.     pivotLong,
  16.     tempLong  : longint
  17.     lower,
  18.     upper,
  19.     middle    : word;
  20.  
  21.   procedure swap(var a,b);
  22.     var x : DirRec absolute a;
  23.         y : DirRec absolute b;
  24.         t : DirRec;
  25.     begin
  26.       t := x;
  27.       x := y;
  28.       y := t;
  29.     end;
  30.  
  31.   begin
  32.     lower := left;
  33.     upper := right;
  34.     middle:= (left + right) div 2;
  35.     case SortType of
  36.       _name: pivotStr   := data[middle].name;
  37.       _ext : pivotStr   := data[middle].ext;
  38.       _size: pivotLong  := data[middle].Lsize;
  39.       _date: pivotLong  := data[middle].Ldate;
  40.     end; { case SortType }
  41.     repeat
  42.       case SortType of
  43.         _name: begin
  44.                  while data[lower].name < pivotStr do inc(lower);
  45.                  while pivotStr < data[upper].name do dec(upper);
  46.                end;
  47.         _ext : begin
  48.                  while data[lower].ext < pivotStr do inc(lower);
  49.                  while pivotStr < data[upper].ext do dec(upper);
  50.                end;
  51.         _size: begin
  52.                  while data[lower].Lsize < pivotLong do inc(lower);
  53.                  while pivotLong < data[upper].Lsize do dec(upper);
  54.                end;
  55.         _date: begin
  56.                  while data[lower].Ldate < pivotLong do inc(lower);
  57.                  while pivotLong < data[upper].Ldate do dec(upper);
  58.                end;
  59.       end; { case SortType }
  60.       if lower <= upper then begin
  61.         swap(data[lower],data[upper]);
  62.         inc(lower);
  63.         dec(upper);
  64.        end;
  65.     until lower > upper;
  66.     if left < upper then quicksort(data,left,upper);
  67.     if lower < right then quicksort(data,lower,right);
  68.   end; { quicksort }
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.